<?php
/***
 * 素材API控制器
 *
 *
 * @author    lanhongjie <lanhj@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */


namespace App\Http\Controllers\Api;


use App\Services\ImgServices\ImgTool;
use App\Services\SourcePicService\SourcePicApiService;
use App\Services\SourcePicService\SourcePicTool;
use App\Services\Tool\GeneralTool;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Response;
use App\Http\Controllers\Controller;
use App\Services\ModifyResourcePriceRecordsService\ModifyResourcePriceRecordsService;

class SourcePicController extends Controller
{
    protected $sourcePicApiSer;
    protected $sourcePicTool;
    protected $modifyResourcePriceRecordsSer;
    /**
     * SourcePicController constructor.
     */
    public function __construct(SourcePicApiService $sourcePicApiSer,
                                ModifyResourcePriceRecordsService $modifyResourcePriceRecordsService,
                                SourcePicTool $sourcePicTool)
    {
        $this->sourcePicApiSer=$sourcePicApiSer;
        $this->sourcePicTool=$sourcePicTool;
        $this->modifyResourcePriceRecordsSer=$modifyResourcePriceRecordsService;
    }

    /***
     * 上传素材
     * @param Request $request
     * @return mixed
     */
    public function uploadSourcePic(Request $request){
        $rule=array(
            'image_url'=>'required|string|',
            'source_pic_name'=>'|string|',
            'user_id'=>'required|integer|',
            'price'=>'numeric|',
            'end_time'=>'required|integer|min:0',
        );
        $validator=SourcePicTool::store($request,$rule);
        $image_url=trim($request->input('image_url'));
        $source_pic_name=trim($request->input('source_pic_name'));
        $user_id=intval($request->input('user_id'));
        $price=floatval($request->input('price'));
        $end_time=intval($request->input('end_time'));
        $stock=intval($request->input('stock'));
        $data=array();
        $val_data['image_url']=$image_url;
        $val_data['user_id']=$user_id;
        $val_data['price']=$price;
        $val_data['begin_time']=time();
        $val_data['end_time']=$end_time;
        $val_data['stock']=$stock;
        $val_data['name']=$source_pic_name;
        $data['is_infinite_time']=empty($end_time)?1:0;
        $data['stock']=empty($stock)?0:round($stock);
        $data['is_infinite_stock']=($data['stock']==0)?1:0;
        $data['begin_time']=empty($begin_time)?time():$begin_time;
        $data['end_time']=empty($end_time)?0:$end_time;
        $data['price']=empty($price)?0:$price;
        $data['image_url']=empty($image_url)?'':$image_url;
        $data['is_shelves']=0;
        $data['user_id']=$user_id;
        $data['audit_value']='审核中';
        $data['audit_state']=1;
        $data['platform']=3;
        $data['name']=$source_pic_name;
        $result=array();
        $user_type=SourcePicTool::getUserType($user_id);
        if($user_type===null){
            $result=GeneralTool::createResult('', 'M03017');
            return Response::json($result);
        }elseif ($user_type===0){
            $result=GeneralTool::createResult('', 'M03018');
            return Response::json($result);
        }
        if ($validator->fails())
        {
            if($validator->errors()->first('stock')){
                $result=GeneralTool::createResult('', 'M03003');
            }elseif($validator->errors()->first('end_time')){
                $result=GeneralTool::createResult('', 'M03004');
            }elseif($validator->errors()->first('price')){
                $result=GeneralTool::createResult('', 'M03054');
            }elseif($validator->errors()->first('image_url')){
                if(empty($val_data['image_url'])){
                    $result=GeneralTool::createResult('', 'M03016');
                }else{
                    $result=GeneralTool::createResult('', 'M03013');
                }
            }elseif($validator->errors()->first('source_pic_name')){
             if(!empty($source_pic_name)){
                 $result=GeneralTool::createResult('', 'M03055');
             }else if(mb_strlen($source_pic_name,'utf8')>10){
                    $result=GeneralTool::createResult('', 'M03057');
                }
            }
            if(sizeof($result)!==0){
                return Response::json($result);
            }
        }
        if(!empty($end_time)&&($end_time<(time()+60*60))){
            $result=GeneralTool::createResult('', 'M03058');
            return Response::json($result);
        }
        $image_url_is_valid=ImgTool::imageUrlIsValid($image_url);
        if(!$image_url_is_valid){
            $result=GeneralTool::createResult('', 'M03015');
            return Response::json($result);
        }
        $has_special_char=GeneralTool::hasSpecialChar($source_pic_name);
        if($has_special_char){
            $result=GeneralTool::createResult('', 'M03056');
            return Response::json($result);
        }
        $source_pic_id=$this->sourcePicApiSer->uploadSourcePic($data);
        if($source_pic_id!==false){
            $source_pic=$this->sourcePicTool->getSourcePicBySourcePicId($source_pic_id,1);
            if(empty($source_pic)){
                $result=GeneralTool::createResult('', 'M03014');
                return Response::json($result);
            }
            $original_price = $source_pic->original_price;
            if($original_price==0){
                $original_price='免费';
            }else{
                $original_price=GeneralTool::priceFormat($source_pic->original_price).'';
            }
            $source_pic->original_price=$original_price;
            $source_pic->preferential_price=$original_price;
            $source_pic->has_preferential =0;
            $source_pic->original_image=ImgTool::getImgUrlByImgPath($source_pic->original_image);
            $source_pic->thumbnail=ImgTool::getImgUrlByImgPath($source_pic->thumbnail);
            $source_pic->small_image=ImgTool::getImgUrlByImgPath($source_pic->small_image);
            $source_pic->audit=SourcePicTool::getAuditByAuditState($source_pic->audit_state);
            $source_pic->audit_value=SourcePicTool::getAuditByAuditState($source_pic->audit_state);
            $source_pic->is_shelves_value=SourcePicTool::getIsShelvesState($source_pic->is_shelves,$source_pic->audit_state);
            $result=SourcePicTool::createResult($source_pic,'提交成功，待审核中','M00000');
        }else{
            $result=GeneralTool::createResult('', 'M03014');
        }
        return Response::json($result);
    }

    /***
     * 更新素材
     * @param Request $request
     * @return mixed
     */
    public function updateSourcePic(Request $request){
        $rule=array(
            'source_pic_id'=>'required|string|',
            'user_id'=>'required|integer|',
            'source_pic_name'=>'|string|',
            'price'=>'numeric|',
            'end_time'=>'required|integer|min:0',
        );
        $validator=SourcePicTool::store($request,$rule);
        $source_pic_id=trim($request->input('source_pic_id'));
        $source_pic_name=trim($request->input('source_pic_name'));
        $user_id=intval($request->input('user_id'));
        $price=floatval($request->input('price'));
        $end_time=intval($request->input('end_time'));
        $stock=intval($request->input('stock'));
        $data=array();
        $val_data['user_id']=$user_id;
        $val_data['price']=$price;
        $val_data['begin_time']=time();
        $val_data['source_pic_id']=$source_pic_id;
        $val_data['stock']=$stock;
        $val_data['name']=$source_pic_name;
        $data['is_infinite_time']=empty($end_time)?1:0;
        $data['is_infinite_stock']=empty($stock)?1:0;
        $data['stock']=empty($stock)?0:round($stock);
        $data['end_time']=empty($end_time)?0:$end_time;
        $data['source_pic_id']=$source_pic_id;
        $data['user_id']=$user_id;
        if(!empty($source_pic_name)){
            $data['name']=$source_pic_name;
        }
        if(empty($user_id)){
            $result=GeneralTool::createResult('', 'M03031');
            return Response::json($result);
        }
        $user_type=SourcePicTool::getUserType($user_id);
        if($user_type===null){
            $result=GeneralTool::createResult('', 'M03017');
            return Response::json($result);
        }elseif ($user_type===0){
            $result=GeneralTool::createResult('', 'M03018');
            return Response::json($result);
        }
        $result=array();
        if ($validator->fails())
        {
            $result=array();
            if($validator->errors()->first('end_time')&&!empty($end_time)){
                $result=GeneralTool::createResult('', 'M03004');
            }elseif($validator->errors()->first('price')){
                $result=GeneralTool::createResult('', 'M03054');
            }elseif($validator->errors()->first('source_pic_name')){
                if(!empty($source_pic_name)){
                    $result=GeneralTool::createResult('', 'M03055');
                }else if(mb_strlen($source_pic_name,'utf8')>10){
                    $result=GeneralTool::createResult('', 'M03057');
                }
            }
            if(sizeof($result)!==0){
                return Response::json($result);
            }
        }
        if(!empty($end_time)&&($end_time<(time()+60*60))){
            $result=GeneralTool::createResult('', 'M03058');
            return Response::json($result);
        }
        $has_special_char=GeneralTool::hasSpecialChar($source_pic_name);
        if($has_special_char){
            $result=GeneralTool::createResult('', 'M03056');
            return Response::json($result);
        }
        $source_pic=$this->sourcePicApiSer->getSourcePicBySourcePicId($source_pic_id);
        if(empty($source_pic)){
            $result=GeneralTool::createResult('', 'M03019');
            return Response::json($result);
        }
        $user_type=SourcePicTool::getUserType($user_id);
        if($user_type===null){
            $result=GeneralTool::createResult('', 'M03017');
        }elseif ($user_type===0){
            $result=GeneralTool::createResult('', 'M03018');
        }else if($source_pic->user_id!==$user_id){
            $result=GeneralTool::createResult('', 'M03020');
        }else if(($source_pic->audit_state!==2)){
            $result=GeneralTool::createResult('', 'M03051');
        }else if(($source_pic->is_shelves!==1)){
            $result=GeneralTool::createResult('', 'M03052');
        }
        if(sizeof($result)!==0){
            return Response::json($result);
        }
        $res=$this->sourcePicApiSer->updateSourcePic($data);
        if($res){
            if ($source_pic->price!==$price){
                $input['resource_type']=1;
                $input['resource_id']=$source_pic_id;
                $input['modify_price']=$price;
                $input['price']= $source_pic->price;//更新价格前的价格
                $input['admin_id']=0;
                $input['admin_email']='';
                @$update_price_recoed_result=$this->modifyResourcePriceRecordsSer->save_modify_resource_price_record($input);
            }
            $result=SourcePicTool::createResult('','修改成功，将在24小时后对所有用户生效！','M00000');
        }else{
            $result=GeneralTool::createResult('', 'M03014');
        }
        return Response::json($result);
    }

    /***
     * 获取素材标签列表
     * @param Request $request
     * @return mixed
     */
    public function postGetTagList(Request $request){
        $rule=array(
            'page'=>'required|integer|min:1',
            'per_page'=>'required|integer|min:1',
            'product_temp_id'=>'required|string|',
        );
        $page=intval($request->input('page'));
        $per_page=intval($request->input('per_page'));
        $product_temp_id=trim($request->input('product_temp_id'));
        $validator=SourcePicTool::store($request,$rule);
        if ($validator->fails())
        {
            if($validator->errors()->first('page')){
                if(!empty($page)){
                    $result = GeneralTool::createResult('','M03021');
                }
            }
            if($validator->errors()->first('per_page')){
                $per_page=10;
            }
            if($validator->errors()->first('product_temp_id')){
                if(!empty($product_temp_id)){
                    $result = GeneralTool::createResult('','M03023');
                }
            }
            if(isset($result)&&sizeof($result)>0){
                return Response::json($result);
            }
        }
        $input['per_page']=$per_page;
        $input['product_temp_id']=$product_temp_id;
        $tag_list=$this->sourcePicApiSer->getTagList($input);
        $result=GeneralTool::createResult($tag_list, 'M00000');
        return Response::json($result);
    }

    /***
     * 获取素材列表
     * @param Request $request
     * @return mixed
     */
    public function getSourcePicList(Request $request){
        $rule=array(
            'page'=>'required|integer|min:1',
            'per_page'=>'required|integer|min:1',
            'tag_id'=>'required|integer|min:1',
            'product_temp_id'=>'required|string|',
        );
        $page=intval($request->input('page'));
        $per_page=intval($request->input('per_page'));
        $tag_id=intval($request->input('tag_id'));
        $product_temp_id=trim($request->input('product_temp_id'));
        $validator=SourcePicTool::store($request,$rule);
        if ($validator->fails())
        {
            if($validator->errors()->first('page')){
                if(!empty($page)){
                    $result = GeneralTool::createResult('','M03021');
                }
            }else if($validator->errors()->first('per_page')){
                $per_page=10;
            }else if($validator->errors()->first('tag_id')){
                if(!empty($tag_id)){
                    $result = GeneralTool::createResult('','M03024');
                }else{
                    $result = GeneralTool::createResult('','M03025');
                }
            }else if($validator->errors()->first('product_temp_id')){
                if(!empty($product_temp_id)){
                    $result = GeneralTool::createResult('','M03023');
                }
            }
            if(isset($result)&&(sizeof($result)>0)){
                return Response::json($result);
            }
        }
        $per_page=empty($per_page)?10:$per_page;
        if($per_page>200){
            $per_page=200;
        }
        $input['per_page']=$per_page;
        $input['tag_id']=$tag_id;
        $input['product_temp_id']=$product_temp_id;
        $source_pics=$this->sourcePicApiSer->getSourcePicByTagId($input);
        $result=GeneralTool::createResult($source_pics, 'M00000');
        return Response::json($result);
    }

    /***
     * 素材上架
     * @param Request $request
     * @return mixed
     */
    public function sourcePicShelves(Request $request){
        $rule=array(
            'user_id'=>'required|integer|min:1',
            'source_pic_id'=>'required|string|',
            'source_pic_name'=>'|string|',
            'price'=>'numeric|',
            'end_time'=>'required|integer|min:0',
        );
        $user_id=intval($request->input('user_id'));
        $source_pic_name=trim($request->input('source_pic_name'));
        $source_pic_id=trim($request->input('source_pic_id'));
        $price=floatval($request->input('price'));
        $end_time=intval($request->input('end_time'));
        $data['end_time']=empty($end_time)?0:$end_time;
        $data['is_infinite_time']=empty($end_time==0)?1:0;
        $data['source_pic_id']=empty($source_pic_id)?'':$source_pic_id;
        $data['user_id']=empty($user_id)?0:$user_id;
        if(!empty($source_pic_name)){
            $data['name']=$source_pic_name;
        }
        $validator=SourcePicTool::store($request,$rule);
        if ($validator->fails())
        {
            if($validator->errors()->first('user_id')){
                $result = GeneralTool::createResult('','M03031');
            }else if($validator->errors()->first('source_pic_id')){
                $result = GeneralTool::createResult('','M03032');
            }else if($validator->errors()->first('source_pic_name')){
                if(!empty($source_pic_name)){
                    $result=GeneralTool::createResult('', 'M03055');
                }else if(mb_strlen($source_pic_name,'utf8')>10){
                    $result=GeneralTool::createResult('', 'M03057');
                }
            }
            if(isset($result)&&sizeof($result)>0){
                return Response::json($result);
            }
        }
        if(!empty($end_time)&&($end_time<(time()+60*60))){
            $result=GeneralTool::createResult('', 'M03058');
            return Response::json($result);
        }
        //查看用户的类型
        if(!isset($result)&&!empty($user_id)){
            $user_type=SourcePicTool::getUserType($user_id);
            if($user_type===null){
                $result=GeneralTool::createResult('', 'M03017');
            }elseif (($user_type===0)){
                $result=GeneralTool::createResult('', 'M03036');
            }
            if(isset($result)&&sizeof($result)>0){
                return Response::json($result);
            }
        }
        if(!isset($result)){
            $source_pic=$this->sourcePicTool->getSourcePicBySourcePicId($source_pic_id);
             //查看当前修改的素材是不是存在
            if(empty($source_pic)){
                $result=GeneralTool::createResult('', 'M03029');
                return Response::json($result);
                //对上传素材的用户和当前修改素材用户进行核对
            }elseif ($source_pic->user_id!=$user_id){
                $result=GeneralTool::createResult('', 'M03020');
                return Response::json($result);
                //对素材审核状态进行判断
            }elseif ($source_pic->audit_state!==2){
                $result=GeneralTool::createResult('', 'M03053');
                return Response::json($result);
            }
        }
        $has_special_char=GeneralTool::hasSpecialChar($source_pic_name);
        if($has_special_char){
            $result=GeneralTool::createResult('', 'M03056');
            return Response::json($result);
        }
        $res=$this->sourcePicApiSer->shelvesSourcePic($data);
        $is_shelves=$this->sourcePicApiSer->getSourcePicIsShelve($source_pic_id);
        if($is_shelves===1){
            if ($source_pic->original_price!==$price){
                $input['resource_type']=1;
                $input['resource_id']=$source_pic_id;
                $input['modify_price']=$price;
                $input['price']= $source_pic->original_price;//更新价格前的价格
                $input['admin_id']=0;
                $input['admin_email']='';
                @$update_price_recoed_result=$this->modifyResourcePriceRecordsSer->save_modify_resource_price_record($input);
            }
            $result=SourcePicTool::createResult(array('is_shelves'=>$is_shelves),'修改成功，将在24小时后对所有用户生效！','M00000');
        }else{
            $result=GeneralTool::createResult(array('is_shelves'=>$is_shelves), 'M03038');
        }
        return Response::json($result);
    }

    /***
     * 删除素材
     * @param Request $request
     * @return mixed
     */
    public function delSourcePic(Request $request){
        $rule=array(
            'user_id'=>'required|integer|min:1',
            'source_pic_id'=>'required|string|',
        );
        $user_id=intval($request->input('user_id'));
        $source_pic_id=trim($request->input('source_pic_id'));
        $data['source_pic_id']=empty($source_pic_id)?'':$source_pic_id;
        $data['user_id']=empty($user_id)?0:$user_id;
        $validator=SourcePicTool::store($request,$rule);
        if ($validator->fails())
        {
            if($validator->errors()->first('user_id')){
                $result = GeneralTool::createResult('','M03031');
                return Response::json($result);
            }
            if($validator->errors()->first('source_pic_id')){
                $result = GeneralTool::createResult('','M03032');
                return Response::json($result);
            }
        }
        //查看用户的类型
        if(!isset($result)&&!empty($user_id)){
            $user_type=SourcePicTool::getUserType($user_id);
            if($user_type===null){
                $result=GeneralTool::createResult('', 'M03017');
                return Response::json($result);
            }elseif (($user_type===0)){
                $result=GeneralTool::createResult('', 'M03036');
                return Response::json($result);
            }
        }
        if(!isset($result)){
            $source_pic=$this->sourcePicTool->getSourcePicBySourcePicId($source_pic_id);
            //查看当前修改的素材是不是存在
            if(empty($source_pic)){
                $result=GeneralTool::createResult('', 'M03029');
                return Response::json($result);
                //对上传素材的用户和当前修改素材用户进行核对
            }elseif ($source_pic->user_id!=$user_id){
                $result=GeneralTool::createResult('', 'M03020');
                return Response::json($result);
                //对素材审核状态进行判断
            }
        }
        $res=$this->sourcePicApiSer->delSourcePic($data);
        $state=$this->sourcePicApiSer->getSourcePicState($source_pic_id);
        if($state===0){
            $result=GeneralTool::createResult(array('state'=>$state), 'M00000');
        }else{
            $result=GeneralTool::createResult(array('state'=>$state), 'M03049');
        }
        return Response::json($result);
    }
    /***
     * 下架素材
     * @param Request $request
     * @return mixed
     */
    public function sourcePicOffShelves(Request $request){
        $rule=array(
            'user_id'=>'required|integer|min:1',
            'source_pic_id'=>'required|string|',
        );
        $user_id=intval($request->input('user_id'));
        $source_pic_id=trim($request->input('source_pic_id'));
        $validator=SourcePicTool::store($request,$rule);
        if ($validator->fails())
        {
            if($validator->errors()->first('user_id')){
                $result = GeneralTool::createResult('','M03031');
                return Response::json($result);
            }else if($validator->errors()->first('source_pic_id')){
                $result = GeneralTool::createResult('','M03032');
                return Response::json($result);
            }
        }
        //查看用户的类型
        if(!isset($result)&&!empty($user_id)){
            $user_type=SourcePicTool::getUserType($user_id);
            if($user_type===null){
                $result=GeneralTool::createResult('', 'M03017');
                return Response::json($result);
            }elseif (($user_type===0)){
                $result=GeneralTool::createResult('', 'M03036');
                return Response::json($result);
            }
        }
        if(!isset($result)){
            $source_pic=$this->sourcePicTool->getSourcePicBySourcePicId($source_pic_id);
            //查看当前修改的素材是不是存在
            if(empty($source_pic)){
                $result=GeneralTool::createResult('', 'M03029');
                return Response::json($result);
                //对上传素材的用户和当前修改素材用户进行核对
            }elseif ($source_pic->user_id!=$user_id){
                $result=GeneralTool::createResult('', 'M03020');
                return Response::json($result);
                //对素材审核状态进行判断
            }elseif ($source_pic->audit_state!==2){
                $result=GeneralTool::createResult('', 'M03051');
                return Response::json($result);
            }
        }
        $source_pic_off_shelve=$this->sourcePicApiSer->offShelvesSourcePic($source_pic_id);
        $is_shelves=$this->sourcePicApiSer->getSourcePicIsShelve($source_pic_id);
        if($is_shelves===0){
            $result=GeneralTool::createResult(array('is_shelves'=>$is_shelves), 'M00000');
        }else{
            $result=GeneralTool::createResult(array('is_shelves'=>$is_shelves), 'M03037');
        }
        return Response::json($result);
    }

    /**
     * 获取我的素材审核列表
     * @param Request $request
     * @return mixed
     */
    public function getMySourcePicAuditList(Request $request){
        $rule=array(
            'page'=>'required|integer|min:1',
            'per_page'=>'required|integer|min:1',
            'user_id'=>'required|integer|',
        );
        $page=intval($request->input('page'));
        $per_page=intval($request->input('per_page'));
        $user_id=intval($request->input('user_id'));
        $validator=SourcePicTool::store($request,$rule);
        if ($validator->fails())
        {
            if($validator->errors()->first('page')){
                if(!empty($page)){
                    $result = GeneralTool::createResult('','M03021');
                    return Response::json($result);
                }
            }
            if($validator->errors()->first('per_page')){
                $per_page=10;
            }
            if($validator->errors()->first('user_id')){
                $result = GeneralTool::createResult('','M03031');
                return Response::json($result);
            }
        }
        $user_type=SourcePicTool::getUserType($user_id);
        if($user_type===null){
            $result=GeneralTool::createResult('', 'M03017');
            return Response::json($result);
        }elseif ($user_type===0){
            $result=GeneralTool::createResult('', 'M03039');
            return Response::json($result);
        }
        $per_page=empty($per_page)?10:$per_page;
        if($per_page>200){
            $per_page=200;
        }
        $data['per_page']=$per_page;
        $data['user_id']=$user_id;
        $source_pics=$this->sourcePicApiSer->getMySourcePicAuditList($data);
        $result=GeneralTool::createResult($source_pics, 'M00000');
        return Response::json($result);
    }

    /***
     * 获取素材已售列表
     * @param Request $request
     * @return mixed
     */
    public function getMySourcePicOnTheShelvesList(Request $request){
        $rule=array(
            'page'=>'required|integer|min:1',
            'per_page'=>'required|integer|min:1',
            'user_id'=>'required|integer|',
        );
        $page=intval($request->input('page'));
        $per_page=intval($request->input('per_page'));
        $user_id=intval($request->input('user_id'));
        $validator=SourcePicTool::store($request,$rule);
        if ($validator->fails())
        {
            if($validator->errors()->first('page')){
                if(!empty($page)){
                    $result = GeneralTool::createResult('','M03021');
                }
            }
            if($validator->errors()->first('per_page')){
                $per_page=10;
            }
            if($validator->errors()->first('user_id')){
                $result = GeneralTool::createResult('','M03031');
            }
        }
        if(isset($result)&&sizeof($result)!==0){
            return Response::json($result);
        }
        $user_type=SourcePicTool::getUserType($user_id);
        if($user_type===null){
            $result=GeneralTool::createResult('', 'M03017');
        }elseif ($user_type===0){
            $result=GeneralTool::createResult('', 'M03018');
        }
        if(isset($result)&&sizeof($result)!==0){
            return Response::json($result);
        }
        $per_page=empty($per_page)?10:$per_page;
        if($per_page>200){
            $per_page=200;
        }
        $data['per_page']=$per_page;
        $data['user_id']=$user_id;
        $source_pics=$this->sourcePicApiSer->getMySourcePicOnTheShelvesList($data);
        $result=GeneralTool::createResult($source_pics, 'M00000');
        return Response::json($result);
    }

    /***
     * 未上架素材列表
     * @param Request $request
     * @return mixed
     */
    public function offShelvesList(Request $request){
        $rule=array(
            'page'=>'required|integer|min:1',
            'per_page'=>'required|integer|min:1',
            'user_id'=>'required|integer|',
        );
        $page=intval($request->input('page'));
        $per_page=intval($request->input('per_page'));
        $user_id=intval($request->input('user_id'));
        $validator=SourcePicTool::store($request,$rule);
        if ($validator->fails())
        {
            if($validator->errors()->first('page')){
                if(!empty($page)){
                    $result = GeneralTool::createResult('','M03021');
                }
            }
            if($validator->errors()->first('per_page')){
                $per_page=10;
            }
            if($validator->errors()->first('user_id')){
                $result = GeneralTool::createResult('','M03031');
            }
        }
        if(isset($result)&&sizeof($result)!==0){
            return Response::json($result);
        }
        $user_type=SourcePicTool::getUserType($user_id);
        if($user_type===null){
            $result=GeneralTool::createResult('', 'M03017');
        }elseif ($user_type===0){
            $result=GeneralTool::createResult('', 'M03018');
        }
        if(isset($result)&&sizeof($result)!==0){
            return Response::json($result);
        }
        $per_page=empty($per_page)?10:$per_page;
        if($per_page>200){
            $per_page=200;
        }
        $data['per_page']=$per_page;
        $data['user_id']=$user_id;
        $source_pics=$this->sourcePicApiSer->getMySourcePicOffTheShelvesList($data);
        $result=GeneralTool::createResult($source_pics, 'M00000');
        return Response::json($result);
    }
    /***
     * 获取我所有素材
     * @param Request $request
     * @return mixed
     */
    public function getMyAllSourcePicList(Request $request){
        $rule=array(
            'page'=>'required|integer|min:1',
            'per_page'=>'required|integer|min:1',
            'user_id'=>'required|integer|',
        );
        $page=intval($request->input('page'));
        $per_page=intval($request->input('per_page'));
        $user_id=intval($request->input('user_id'));
        $validator=SourcePicTool::store($request,$rule);
        if ($validator->fails())
        {
            if($validator->errors()->first('page')){
                if(!empty($page)){
                    $result = GeneralTool::createResult('','M03021');
                }
            }
            if($validator->errors()->first('per_page')){
                $per_page=10;
            }
            if($validator->errors()->first('user_id')){
                $result = GeneralTool::createResult('','M03031');
            }
            if(isset($result)&&sizeof($result)!==0){
                return Response::json($result);
            }
        }
        $user_type=SourcePicTool::getUserType($user_id);
        if($user_type===null){
            $result=GeneralTool::createResult('', 'M03017');
            return Response::json($result);
        }elseif ($user_type===0){
            $result=GeneralTool::createResult('', 'M03039');
            return Response::json($result);
        }
        $per_page=empty($per_page)?10:$per_page;
        if($per_page>200){
            $per_page=200;
        }
        $data['per_page']=$per_page;
        $data['user_id']=$user_id;
        $source_pics=$this->sourcePicApiSer->getMyAllSourcePicList($data);
        $result=GeneralTool::createResult($source_pics, 'M00000');
        return Response::json($result);
    }
}
